<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<script>
    const map = new Map([['name', 'zhang'], ['age', 19]]);
    const set = new Set([1, 2, 3, 4]);
    const arr = [10,20,30];

    // map.forEach((item, index, obj) => console.log(item, index, obj));
    // set.forEach((item, index, obj) => console.log(item, index, obj));
    arr.forEach((item, index, obj) => console.log(item, index, obj));

    function myForEach(obj, cb) {
        const type = Object.prototype.toString.call(obj);
        let index = 0;
        for (const key of obj) {
            if (type === '[object Map]') {
                cb && cb(key[1], key[0], obj);
            } else if (type === '[object Set]') {
                cb && cb(key, key, obj);
            } else if (type === '[object Array]') {
                cb && cb(key, index++, obj);
            }
        }
    }

    function myForEachV2(obj, cb) {
        const iterator = obj[Symbol.iterator]();
        let res = iterator.next();
        cb && cb(res);
        while(!res.done) {
            res = iterator.next();
            cb && cb(res);
        }
    }

    myForEach(map, (item, index, obj) => console.log(item, index, obj));
    myForEach(set, (item, index, obj) => console.log(item, index, obj));
    myForEach(arr, (item, index, obj) => console.log(item, index, obj));
</script>
</body>
</html>